package com.ride.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ride.dao.ProfileDao;
import com.ride.dao.RideDao;
import com.ride.dao.RideSearchDao;
import com.ride.dao.UserDao;
import com.ride.model.Operator;
import com.ride.model.Profile;
import com.ride.model.Ride;
import com.ride.model.RideSearch;
import com.ride.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * TODO document me!
 *
 * @version $LastChangedRevision: 16 $
 *          <p/>
 *          $LastChangedBy: marcel.menze@gmail.com $
 *          <p/>
 *          $LastChangedDate: 2011-04-05 12:34:26 +0000 (Tue, 05 Apr 2011) $
 */
@Service("userService")
public class UserServiceImpl implements UserService {

    private UserDao userDao;
    private ProfileDao profileDao;
    private RideSearchDao rideSearchDao;
    private RideDao rideDao;

    @Autowired
    public UserServiceImpl(UserDao userDao, ProfileDao profileDao, RideSearchDao rideSearchDao, RideDao rideDao) {
        this.userDao = userDao;
        this.profileDao = profileDao;
        this.rideSearchDao = rideSearchDao;
        this.rideDao = rideDao;
    }

    @Transactional
    public void createUser(Operator operator) {
        userDao.persist(operator);
    }

    @Transactional
    public void createUser(String username, String password, String mobilePhone, String email) {
        Operator operator = new Operator();
        Profile profile = new Profile();
        RideSearch rideSearch = new RideSearch();
        Ride ride = new Ride();
        ride.setFinished(false);
        ride.setStarted(false);
        rideDao.persist(ride);
        ArrayList<Ride> rides = new ArrayList<Ride>();
        rides.add(ride);
        profile.setRides(rides);
        rideSearchDao.persist(rideSearch);
        ArrayList<RideSearch> rideSearches = new ArrayList<RideSearch>();
        rideSearches.add(rideSearch);
        profile.setRideSearches(rideSearches);
        operator.setUsername(username);
        operator.setPassword(password);
        operator.setDisabled(false);
        profile.setMobilePhone(mobilePhone);
        profile.setEmail(email);
        profileDao.persist(profile);
        operator.setProfile(profile);
        userDao.persist(operator);
    }

    @Transactional(readOnly = true)
    public List<Operator> findAll() {
        return userDao.findAllUsers();
    }

    @Transactional(readOnly = true)
    public Operator findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    @Transactional
    public void update(Operator operator) {
        userDao.update(operator);
    }

    @Transactional
    public void remove(Operator operator) {
        userDao.delete(operator);
    }

    @Transactional(readOnly = true)
    public Operator findById(Long id) {
        return userDao.findById(id);
    }
}
